*------------------------------------------------------------------------------
* In-school survey data cleaning 
*==============================================================================
* age
gen age = s1

* sex
gen sex = s2 
replace sex = 0 if missing(s2)

* school-grade
gen grade = s3

* race
gen white = s6a
gen black = s6b
gen asian = s6c
gen indian = s6d
gen other = s6e
gen hispanic = s4
recode hispanic (8 = 0)

egen multirace = rowtotal(white black indian asian other hispanic)
tab multirace

gen race = .
replace race = 1 if white == 1 & multirace == 1
replace race = 2 if black == 1 & multirace == 1
replace race = 3 if hispanic == 1
replace race = 4 if asian == 1 & multirace == 1
replace race = 5 if race == . & multirace > 0 // all others (multirace + other races)
replace race = . if multirace == 0

* family structure
gen family = .
replace family = 1 if s11 == 1 & s17 == 1 // two-parent family
replace family = 2 if s11 == 1 & s17 == 0 // single-mother
replace family = 2 if s11 == 1 & missing(s17) // single-mother : missing father?
replace family = 3 if s11 == 0 & s17 == 1 // single-father
replace family = 3 if missing(s11) & s17 == 1 // single-father : missing mother?
replace family = 4 if s11 == 0 & s17 == 0 // no parents

* us-born
gen usborn = s8

* any parent = foreignborn
gen parent_foreign = 0 if ~missing(s13) | ~missing(s19)
replace parent_foreign = 1 if s13 == 0 | s19 == 0

* immigrant status
gen secondG = 0
replace secondG = 1 if s13 == 0 
replace secondG = 1 if s19 == 0
replace secondG = . if missing(s13) & missing(s19)

gen immigrant = .
replace immigrant = 1 if s8 == 0 & (s13 == 0 | s19 ==0) & family == 1 // first generation : two-parent
replace immigrant = 1 if s8 == 0 & s13 == 0 & family == 2 // first generation : single mother
replace immigrant = 1 if s8 == 0 & s19 == 0 & family == 3 // first generation  : single father

replace immigrant = 2 if s8 == 1 & (s13 == 0 | s19 ==0) & family == 1 // second generation : two-parent
replace immigrant = 2 if s8 == 1 & s13 == 0 & family == 2 // second generation : single mother
replace immigrant = 2 if s8 == 1 & s19 == 0 & family == 3 // second generation  : single father

replace immigrant = 3 if s8 == 1 & (s13 == 1 & s19 == 1) & family == 1 // third generation : two-parent
replace immigrant = 3 if s8 == 1 & s13 == 1 & family == 2 // third generation : single mother
replace immigrant = 3 if s8 == 1 & s19 == 1 & family == 3 // third generation : single father

replace immigrant = 4 if s8 == 1 & family == 4 // no parent - second or third generation

* mother / father's education
gen ma_educ = s12
gen fa_educ = s18
recode ma_educ (1/6 10=0) (7/8=1) (9 11=.), gen(ma_college) // 9 or 11 should be missing?
recode fa_educ (1/6 10=0) (7/8=1) (9 11=.), gen(fa_college) // 9 or 11 should be missing?

recode ma_educ (1=8) (2 3 4=12) (5=13) (6=14) (7=16) (8=18) (9=.) (10=0) (11=.)
recode fa_educ (1=8) (2 3 4=12) (5=13) (6=14) (7=16) (8=18) (9=.) (10=0) (11=.)

egen pa_maxeduc = rowmax(ma_educ fa_educ)

* father/mother attachments;
gen ma_care = s16
gen fa_care = s22

egen pa_avgcare = rowmean(ma_care fa_care)

* other extra curriculars
gen club_activity = 1-s44
egen club_n = rowtotal(s44a*)

* Depression
egen depress = rowmean(s60i s60j s60k s60l s60m s60n s60o)

* school attachment
gen s_att1 = 6-s62b // feel close to people at school
gen s_att2 = 6-s62e // feel like part of school
gen s_att3 = 6-s62i // happy to be at school

alpha s_att*, gen(s_attachment)

drop s1-s68

